package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.SecurityControl;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.basic.AlipayPayOrder;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.basic.WeixinPayOrder;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacketOrder;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacketOrderRefund;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AlipayPayOrderService;
import cc.yugu.yhdw.webserver.service.basic.WeixinPayOrderService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePacketOrderRefundService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePacketOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 包月订单退款
 */
@Controller
@RequestMapping(value = "/security/hdw/exchange_packet_order_refund")
public class ExchangePacketOrderRefundController extends SecurityController {

    @Autowired
    ExchangePacketOrderService exchangePacketOrderService;
    @Autowired
    ExchangePacketOrderRefundService exchangePacketOrderRefundService;
    @Autowired
    AlipayPayOrderService alipayPayOrderService;
    @Autowired
    WeixinPayOrderService weixinPayOrderService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_13.getValue());
        model.addAttribute("RefundStatus", ExchangePacketOrderRefund.RefundStatus.values());
        model.addAttribute("APPLY_REFUND", ExchangePacketOrderRefund.RefundStatus.APPLY_REFUND.getValue());
    }
    /**
     * 表格刷新（权限new）
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/7/31
     */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangePacketOrderRefund search, HttpServletRequest request) {
         /*
           查看换电套餐退订订单权限设置
           1.平台（总部）
           2.运营商
           3.代理商
         */
        int type = getSessionUser(request).getType();
        // 代理商
        if (type == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        // 运营商
        if (type == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(exchangePacketOrderRefundService.findPage(search));
    }

    @RequestMapping(value = "weixin_pay_order_refund.htm")
    public void weixinPayOrderRefund(Model model, String id) {
        model.addAttribute("sourceId", id);
    }

    @RequestMapping("weixin_pay_order_page_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult weixinPayOrderPageRefund(WeixinPayOrder search) {
        search.setSourceType(WeixinPayOrder.SourceType.EXCHANGE_PACKET_ORDER_PAY.getValue());
        search.setOrderStatus(WeixinPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(weixinPayOrderService.findPage(search));
    }

    @RequestMapping(value = "alipay_pay_order_refund.htm")
    public void alipayPayOrderRefund(Model model, String id) {
        model.addAttribute("sourceId", id);
    }

    @RequestMapping("alipay_pay_order_page_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult alipayPayOrderPageRefund(AlipayPayOrder search) {
        search.setSourceType(AlipayPayOrder.SourceType.EXCHANGE_PACKET_ORDER_PAY.getValue());
        search.setOrderStatus(AlipayPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(alipayPayOrderService.findPage(search));
    }

    @RequestMapping(value = "view.htm")
    public String view(Model model, String id) {
        ExchangePacketOrderRefund entity = exchangePacketOrderRefundService.find(id);
        ExchangePacketOrder packetPeriodOrder = exchangePacketOrderService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
            model.addAttribute("packetPeriodOrder", packetPeriodOrder);
        }

        return "/security/hdw/exchange_packet_order_refund/view";
    }

    @RequestMapping(value = "view_basic.htm")
    public String viewBasic(Model model, String id) {
        ExchangePacketOrderRefund entity = exchangePacketOrderRefundService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        model.addAttribute("RefundStatus", ExchangePacketOrderRefund.RefundStatus.values());
        return "/security/hdw/exchange_packet_order_refund/view_basic";
    }

    @RequestMapping(value = "edit_refund.htm")
    public String editRefund(Model model, String id) {
        ExchangePacketOrderRefund entity = exchangePacketOrderRefundService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/exchange_packet_order_refund/edit_refund";
    }

    @RequestMapping("refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult refund(HttpSession httpSession, ExchangePacketOrderRefund packetPeriodOrderRefund) {
        SessionUser sessionUser = getSessionUser(httpSession);
        String userName = sessionUser.getUsername();
        return  exchangePacketOrderRefundService.refund(userName, packetPeriodOrderRefund);
    }

    /**
     * 拒绝退款
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "edit_repulse_refund.htm")
    public String editRepulseRefund(Model model, String id) {
        ExchangePacketOrderRefund entity = exchangePacketOrderRefundService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/exchange_packet_order_refund/edit_repulse_refund";
    }

    @RequestMapping("repulse_refund.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult repulseRefund(HttpSession httpSession, ExchangePacketOrderRefund packetPeriodOrderRefund) {
        SessionUser sessionUser = getSessionUser(httpSession);
        String userName = sessionUser.getUsername();
        return exchangePacketOrderRefundService.repulseRefund(userName, packetPeriodOrderRefund);
    }


}
